package com.applabs.dnd.ui;

//import com.example.android.animationsdemo.MainActivity;
//import com.example.android.animationsdemo.R;

import com.applabs.dnd.core.Constant;
import com.applabs.dnd.core.DNDCallSettings;
import com.applabs.dnd.core.DNDMeProfile;
import com.applabs.dnd.core.DNDMeProfileManager;
import com.applabs.dnd.core.DNDMsgSettings;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Profiles extends Activity {

	private final int SAVED = 1;
	int type = Constant.ADD;
    private ViewGroup mContainerView;
    private DNDMeProfileManager pManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(com.applabs.dnd.R.layout.activity_profiles);
        mContainerView = (ViewGroup) findViewById(com.applabs.dnd.R.id.container);
        pManager = new DNDMeProfileManager(getApplicationContext());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(com.applabs.dnd.R.menu.activity_profiles, menu);
		return true;
	}

	   public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	            case android.R.id.home:
	                // Navigate "up" the demo structure to the launchpad activity.
	                // See http://developer.android.com/design/patterns/navigation.html for more.
	//                NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
	                return true;

	            case com.applabs.dnd.R.id.action_add_item:
	                // Hide the "empty" view since there is now at least one item in the list.
	                findViewById(android.R.id.empty).setVisibility(View.GONE);
	                startEditActivity(Constant.ADD);
	                //addItem();
	                
	                return true;
	        }

	        return super.onOptionsItemSelected(item);
	    }

	    private void addItem(String name, int type) {
	        // Instantiate a new "row" view.
	        final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
	        		com.applabs.dnd.R.layout.list_item_example, mContainerView, false);

	        // Set the text in the new row to a random country.
//	        ((TextView) newView.findViewById(android.R.id.text1)).setText(
//	                COUNTRIES[(int) (Math.random() * COUNTRIES.length)]);
	        ((TextView) newView.findViewById(android.R.id.text1)).setText(
	                name);
	       
	       
	        ((TextView)newView.findViewById(android.R.id.text1)).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String name = (String) ((TextView) v.findViewById(android.R.id.text1)).getText();
					startEditActivity(Constant.EDIT, name);
					
				}
			});

	        // Set a click listener for the "X" button in the row that will remove the row.
	        newView.findViewById(com.applabs.dnd.R.id.delete_button).setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View view) {
	                // Remove the row from its parent (the container view).
	                // Because mContainerView has android:animateLayoutChanges set to true,
	                // this removal is automatically animated.
	                mContainerView.removeView(newView);
	                String name = (String) ((TextView) newView.findViewById(android.R.id.text1)).getText();
	                pManager.removeProfile(name);
	                // If there are no rows remaining, show the empty view.
	                if (mContainerView.getChildCount() == 0) {
	                    findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
	                }
	            }
	        });

	        // Because mContainerView has android:animateLayoutChanges set to true,
	        // adding this view is automatically animated.
	        mContainerView.addView(newView, 0);
	    }
	    
	    private void startEditActivity(int type)
	    {
	    	Intent intent2 = new Intent(this, AddEditProfile.class);
	    	intent2.putExtra("Type", type);
	    	startActivityForResult(intent2, 1);
	    }
	    
	    private void startEditActivity(int type, String profileName)
	    {
	    	Intent intent2 = new Intent(this, AddEditProfile.class);
	    	intent2.putExtra("Type", type);
	    	intent2.putExtra("Name", profileName);
	    	startActivityForResult(intent2, 1);
	    }

	    @Override
		protected void onActivityResult(int requestCode, int resultCode,
				Intent data) {
			// TODO Auto-generated method stub
			super.onActivityResult(requestCode, resultCode, data);
			Log.i("Profiles:","Inside OnACtivityResult");
			if((resultCode == RESULT_OK) && (requestCode == 1))
			{
				Log.i("Profile","result is OK");
				int type = data.getIntExtra("type", -1);
				int status = data.getIntExtra("status",-1);
				String profile = data.getStringExtra("profile");
				int callRule = data.getIntExtra("callRule",-1);
				int msgRule = data.getIntExtra("msgRule",-1);
				int callRejectType = data.getIntExtra("callRejectType",-1);
				int msgRejectType = data.getIntExtra("msgRejectType",-1);
				if((type == Constant.ADD) && (status == SAVED))
				{
					Log.i("Profiles","result is saved");
					addItem(profile, type);
				}else if(!(data.getStringExtra("oldProfileName")).equals(profile))
				{
					Log.i("Profiles:","new profile is created");
					addItem(profile,Constant.ADD);
				}else
				{
					Log.i("Profiles:","new profile is not created");
				}
				save(profile, callRule, msgRule, callRejectType, msgRejectType);
			}
		}
	    
	    
	    private void save(String name, int callRule, int msgRule, int callRejectType, int msgRejectType)
	    {
	    	DNDCallSettings callSetting = new DNDCallSettings(null, callRule, callRejectType, -1, -1, null);
	    	DNDMsgSettings msgSetting = new DNDMsgSettings(msgRule, msgRejectType);
    		DNDMeProfile newProfile = new DNDMeProfile(name, callSetting, msgSetting);

	    	if( null == pManager.getProfile(name) )
	    	{
	    		pManager.createProfile(newProfile);
	    	}
	    	else
	    	{
	    		pManager.updateProfile(name, newProfile);
	    	}
	    }
		/**
	     * A static list of country names.
	     */
	    private static final String[] COUNTRIES = new String[]{
	            "Default", "Meeting", "Sleeping", "Gaming", 
	    };

}
